package com.example.x6.configurationmaintenance.Utils;

import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Vector;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class SystemBuildProp {
    private static SystemBuildProp systemBuildProp;
    private final String TAG = getClass().getSimpleName();
    private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    public static SystemBuildProp getSystemBuildProp() {
        synchronized (SystemBuildProp.class) {
            if (systemBuildProp == null) {
                systemBuildProp = new SystemBuildProp();
            }
        }
        return systemBuildProp;
    }

    private SystemBuildProp() {
    }

    public String getProp(String key) {
        if (key == null) {
            return null;
        }
        try {
            FileInputStream fileInputStream = new FileInputStream("/system/build.prop");
            if (fileInputStream == null) {
                return null;
            }
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
            while (true) {
                String line = bufferedReader.readLine();
                if (line == null) {
                    return null;
                }
                String[] tempLine = line.split("=");
                if (tempLine.length == 2 && tempLine[0].trim().equals(key.trim())) {
                    return tempLine[1].trim();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e2) {
            e2.printStackTrace();
            return null;
        }
    }

    public boolean setProp(String key, String value) {
        IOException e;
        String tempFilePath;
        if (key == null || value == null) {
            Log.e(this.TAG, "key or value invalid!!!");
            return false;
        }
        String tempFilePath2 = "/sdcard/tempFile";
        this.readWriteLock.writeLock().lock();
        String permission = getFilePermission("/system/build.prop");
        if (permission == null) {
            this.readWriteLock.writeLock().unlock();
            Log.e(this.TAG, "get build.prop file permission failed!!!");
            return false;
        }
        SuCommand suCommand = new SuCommand();
        if (suCommand.execRootCmdSilent("mount -o rw,remount /system") != 0) {
            this.readWriteLock.writeLock().unlock();
            Log.e(this.TAG, "mount system failed!!!");
            return false;
        }
        File tempFile = new File(tempFilePath2);
        try {
            if (!tempFile.exists()) {
                try {
                    tempFile.createNewFile();
                } catch (IOException e2) {
                    e = e2;
                }
            }
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("/system/build.prop")));
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile)));
            boolean isRoDevInfoExisted = false;
            while (true) {
                String readLine = bufferedReader.readLine();
                String tmpString = readLine;
                if (readLine == null) {
                    break;
                }
                if (tmpString.contains(key)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(key);
                    tempFilePath = tempFilePath2;
                    sb.append("=");
                    sb.append(value);
                    tmpString = sb.toString();
                    isRoDevInfoExisted = true;
                } else {
                    tempFilePath = tempFilePath2;
                }
                bufferedWriter.write(tmpString + "\n");
                tempFilePath2 = tempFilePath;
            }
            if (!isRoDevInfoExisted) {
                bufferedWriter.write(key + "=" + value + "\n");
            }
            bufferedWriter.flush();
            bufferedReader.close();
            bufferedWriter.close();
            if (suCommand.execRootCmdSilent("cp " + tempFile + " " + "/system/build.prop") != 0) {
                this.readWriteLock.writeLock().unlock();
                Log.e(this.TAG, "copy temp file to build.prop failed!!!");
                tempFile.delete();
                return false;
            }
            tempFile.delete();
            if (suCommand.execRootCmdSilent("chmod " + permission + " " + "/system/build.prop") != 0) {
                this.readWriteLock.writeLock().unlock();
                Log.e(this.TAG, "change new build.prop permission failed");
                return false;
            }
            suCommand.execRootCmdSilent("sync");
            this.readWriteLock.writeLock().unlock();
            return true;
        } catch (IOException e4) {
            e = e4;
            e.printStackTrace();
            this.readWriteLock.writeLock().unlock();
            Log.e(this.TAG, e.getMessage());
            return false;
        }
    }

    private String getFilePermission(String path) {
        if (path == null) {
            return null;
        }
        SuCommand suCommand = new SuCommand();
        if (suCommand.execRootCmdSilent("ls " + path) != 0) {
            return null;
        }
        Vector<String> reslult = suCommand.execRootCmd("ls " + path + " -l");
        if (reslult == null || reslult.size() == 0) {
            return null;
        }
        String permissionString = reslult.get(0).split(" ")[0].trim();
        int rootPermission = 0;
        int groupPermission = 0;
        int normalPermission = 0;
        if (permissionString.charAt(1) == 'r') {
            rootPermission = 0 + 4;
        }
        if (permissionString.charAt(2) == 'w') {
            rootPermission += 2;
        }
        if (permissionString.charAt(3) == 'x') {
            rootPermission++;
        }
        if (permissionString.charAt(4) == 'r') {
            groupPermission = 0 + 4;
        }
        if (permissionString.charAt(5) == 'w') {
            groupPermission += 2;
        }
        if (permissionString.charAt(6) == 'x') {
            groupPermission++;
        }
        if (permissionString.charAt(7) == 'r') {
            normalPermission = 0 + 4;
        }
        if (permissionString.charAt(8) == 'w') {
            normalPermission += 2;
        }
        if (permissionString.charAt(9) == 'x') {
            normalPermission++;
        }
        return String.valueOf(rootPermission) + String.valueOf(groupPermission) + String.valueOf(normalPermission);
    }
}
